<?php

namespace controllers;

use EasyWeChat\Factory;
use models\BaseModel;
use models\Users;
use system\Base;

class Common extends Base {
    /**
     * 设置配置
     */
    public static function setConfig() {
        $model = new \models\Config();
        $list  = $model->getConfigs();
        Config($list);
    }

    /**
     * 获取公众号操作对象
     * @return \EasyWeChat\MiniProgram\Application
     */
    public static function getApp() {
        $config = [
            'app_id'        => envg('pay_appid'),
            'secret'        => envg('pay_appsercet'),
            'token'         => envg('token'),
            'response_type' => 'array',
            'debug'         => true,
            'log'           => [
                'level'      => 'error',
                'permission' => 0777,
                'file'       => RunTime . '/tmp/easywechat.log',
            ],
            'http'          => [
                'max_retries' => 5,
                'retry_delay' => 500,
                'timeout'     => 30,
            ],
        ];
        $app    = Factory::miniProgram($config);

        return $app;
    }

    /**
     * 获取支付对象
     * @return \EasyWeChat\Payment\Application
     */
    public static function getPay() {
        $config = [
            'app_id'     => envg('pay_appid'),
            'mch_id'     => envg('pay_mchid', '1111'),
            'key'        => envg('pay_password', '11'),
            'cert_path'  => ROOT_PATH . 'apiclient_cert.pem',
            'key_path'   => ROOT_PATH . 'apiclient_key.pem',
            'notify_url' => '',
            //            'sandbox'    => true
        ];
        $app    = Factory::payment($config);

        return $app;
    }
}